package logic

import (
	"context"
	"errors"
	"furnipack/furnipackrpc/furnipackrpc"
	"furnipack/furnipackrpc/internal/svc"
	"furnipack/model/mysql"
	"github.com/zeromicro/go-zero/core/logx"
)

type FurnAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFurnAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FurnAddLogic {
	return &FurnAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *FurnAddLogic) FurnAdd(in *furnipackrpc.FurnAddRequest) (*furnipackrpc.FurnAddResponse, error) {
	// todo: add your logic here and delete this line

	title := mysql.Titler{
		TitleName: in.TitleName,
	}
	err := title.TitleFind(mysql.DB, in.TitleName)
	if err != nil {
		return nil, err
	}
	typer := &mysql.Typer{
		TyperName: in.TyperName,
	}
	err = typer.TyperFind(mysql.DB, in.TyperName)
	if err != nil {
		return nil, err
	}

	data := &mysql.Furn{
		TitleId: int(title.ID),
		Name:    in.Name,
		TyperId: int(typer.ID),
	}
	err = data.FurnAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("添加失败")
	}
	return &furnipackrpc.FurnAddResponse{
		FurnId: int64(data.ID),
	}, nil
}
